package com.lzy.webapi.controller;

import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.tools.kv.KvClient;
import com.lzy.product.common.constants.RedisPrefixConstants;
import com.lzy.webapi.component.UserToken;
import com.lzy.webapi.facade.AgentOrderFacade;
import com.lzy.webapi.request.AgentOrderParamRequest;
import com.lzy.webapi.request.AgentOrderRequest;
import com.lzy.webapi.request.OrderPayCodeRequest;
import com.lzy.webapi.response.AgentOrderResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/api/agentOrder")
public class AgentOrderController extends BaseController {

    @Autowired
    private AgentOrderFacade agentOrderFacade;

    @Autowired
    private KvClient kvClient;

    /**
     * 创建协议订单
     *
     * @return
     */
    @RequestMapping(value = "/createProtocolOrder", method = RequestMethod.POST)
    @ResponseBody
    public Result createProtocolOrder(@RequestHeader("token") String token, AgentOrderRequest agentOrderRequest) {
        UserToken userToken = isLogin(token);
        return new Result(agentOrderFacade.createProtocolOrder(userToken.getUserId(), agentOrderRequest));
    }

    /**
     * 创建产品订单
     * deviceType 1：2.1端 2：Ipad端
     *
     * @param token
     * @param type              1开启余额支付 2关闭余额支付
     * @param agentOrderRequest
     * @return
     */
    @RequestMapping(value = "/createProductOrder/{deviceType}/{type}", method = RequestMethod.POST)
    @ResponseBody
    public Result createProductOrder(@PathVariable("type") Integer type, @PathVariable("deviceType") Integer deviceType, @RequestHeader("token") String token, AgentOrderRequest agentOrderRequest) {
        Ensure.that(deviceType).isNotNull("06000003");
        Ensure.that(deviceType.equals(1) || deviceType.equals(2)).isTrue("06000004");
        UserToken userToken = new UserToken();
        //如果是从2.1下单，从redis取得2.1的token
        if (deviceType.equals(1)) {
            userToken = kvClient.read(RedisPrefixConstants.TOKEN_KEY + token, UserToken.class);
            Ensure.that(userToken == null).isFalse("00001014");
        } else {
            userToken = isLogin(token);
        }
        return new Result(agentOrderFacade.createProductOrder(userToken.getUserId(), agentOrderRequest, type));
    }


    /**
     * 获取支付二维码地址
     *
     * @param token
     * @param request
     * @return
     */
    @RequestMapping(value = "payCode", method = RequestMethod.GET)
    @ResponseBody
    public Result payCode(@RequestHeader("token") String token, OrderPayCodeRequest request) {
        Ensure.that(request).isNotNull("04000001");
        Ensure.that(request.getOrderId()).isNotNull("06000006");
        Ensure.that(request.getPayWay().equals(BigDecimal.ONE) || request.getPayWay().equals(BigDecimal.ZERO)).isTrue("06000018");
        isLogin(token);
        String payCode = agentOrderFacade.payCode(request.getOrderId(), request.getPayWay().intValue());
        Ensure.that(payCode).isNotBlank("06000013");
        return new Result(payCode);
    }

    /**
     * 订单详情
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/{orderId}", method = RequestMethod.GET)
    @ResponseBody
    public Result orderDetail(@PathVariable Long orderId/*, @RequestHeader("token") String token*/) {
        //isLogin(token);
        return new Result(agentOrderFacade.orderDetail(orderId));
    }

    /**
     * 订单列表
     *
     * @param type
     * @param request
     * @return
     */
    @RequestMapping(value = "/orderList/{type}", method = RequestMethod.GET)
    @ResponseBody
    public Result orderList(@PathVariable Integer type, AgentOrderParamRequest request, @RequestHeader("token") String token) {
        UserToken userToken = isLogin(token);
        List<AgentOrderResponse> list = agentOrderFacade.query(request, type, userToken.getUserId());
        return new Result(list);
    }

    /**
     * 价格计算
     *
     * @return
     */
    @RequestMapping(value = "/priceCalculation/{productId}/{num}/{type}")
    @ResponseBody
    public Result priceCalculation(@PathVariable("productId") Long productId, @PathVariable("num") Integer num, @PathVariable("type") Integer type, @RequestHeader("token") String token) {
        UserToken userToken = isLogin(token);
        return new Result(agentOrderFacade.priceCalculation(userToken.getUserId(), productId, num, type));
    }

}
